package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.exception.LoginException;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.utile.HandleFlag;
import com.bjpowernode.crm.utile.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.Map;

/**
 * ClassName:UserController
 * package:com.bjpowernode.crm.settings.web.controller
 * Description:
 *
 * @date: 2022-03-13 17:37
 * @author：lin
 */
@Controller
@RequestMapping("/settings/user")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/login.do")
    @ResponseBody //返回json
    public Map<String,Object> login(String loginAct, String loginPwd, String flag, HttpServletResponse response,
                                    HttpServletRequest request) throws LoginException {
        //接收方式
        /*

        关于方法的返回值类型的设计
        前端需要的是{success:true/false,msg:?}这两项信息
        一般情况下，我们都是将需要返回的多项信息处理成为map集合
        然后返回map

     */
        //密码转换
        loginPwd = MD5Util.getMD5(loginPwd);

        //取得浏览器ip地址
        String ip = request.getRemoteAddr();

         User user = userService.login(loginAct,loginPwd,ip);


        //成功将user保存到session域对象中
        request.getSession().setAttribute("user",user);



        if ("a".equals(flag)){
            Cookie cookie1 =new Cookie("loginAct",loginAct);
            Cookie cookie2 =new Cookie("loginPwd",loginPwd);

            cookie1.setPath("/");
            cookie2.setPath("/");

            cookie1.setMaxAge(60*60*24*10);
            cookie2.setMaxAge(60*60*24*10);

            response.addCookie(cookie1);
            response.addCookie(cookie2);
        }


        return HandleFlag.successTrue();


    }



        @RequestMapping("/toLogin.do")
        public String toLogin (HttpServletRequest request){

        String ip = request.getRemoteAddr();

        Cookie cookie []=request.getCookies();


        if (cookie!=null && cookie.length>0){

            String loginAct =null;
            String loginPwd =null;

            for (Cookie cookie1 : cookie) {

               String name =cookie1.getName();

               if ("loginAct".equals(name)){
                   loginAct = cookie1.getValue();

                   continue;

               }

               if ("loginPwd".equals(name)){
                   loginPwd = cookie1.getValue();
               }


            }
            if (loginAct!=null && loginPwd!=null){

                try {
                    User user=userService.login(loginAct,loginPwd,ip);
                    request.getSession().setAttribute("user",user );

                    //return "redirect:/workbench/toIndex.do";
                   return "redirect:/workbench/toIndex.do";


                } catch (LoginException e) {


                    return "/login";
                }


            }else {



                return "/login";
            }




        }else {


            return "/login";

        }


        }


        @RequestMapping("/logout.do")
        public String logOut(HttpServletResponse response, HttpSession session){

            //销毁session
            session.invalidate();

            Cookie cookie1 =new Cookie("loginAct",null);
            Cookie cookie2 =new Cookie("loginPwd",null);

            cookie1.setPath("/");
            cookie2.setPath("/");

            cookie1.setMaxAge(0);
            cookie2.setMaxAge(0);

            response.addCookie(cookie1);
            response.addCookie(cookie2);

        return  "/login";

        }















}
